package com.cnofd.demo;

import java.io.File;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cnofd.agent.OfdConvertorAgent;
import com.cnofd.agent.OfdSdkInit;
import com.cnofd.agent.PdfConvertorAgent;
import com.cnofd.demo.util.ConfigUtil;
import com.cnofd.ofd.model.ResultInfo;

/**
 * @ClassName: OfdConvertorAgentTest
 * @Description: PDF、Word、Excel、PPT转OFD功能测试
 * @author cnofd
 * @date 2022年1月22日
 */
public class OfdConvertorAgentTest {
    private static final Logger logger = LoggerFactory.getLogger(OfdConvertorAgentTest.class);

    private static String cfgLicenseFile;
    private static String cfgTempDir;
    private static String cfgWorkDir;
    private static String cfgDataBasePath;

    private static String cfgOfficeEngine;
    private static String cfgHtmlEngine;

    private static String sDataPath;
    private static String sOutPath;

    static long start = System.currentTimeMillis();

    public static void main(String[] args) {
        System.out.println("01-启动：" + System.currentTimeMillis());

        // 读取配置文件
        Properties propConfig = ConfigUtil.getConfigProperties("init");
        cfgLicenseFile = ConfigUtil.getConfigKey(propConfig, "LicenseFile");
        cfgTempDir = ConfigUtil.getConfigKey(propConfig, "TempDir");
        cfgWorkDir = ConfigUtil.getConfigKey(propConfig, "WorkDir");
        cfgDataBasePath = ConfigUtil.getConfigKey(propConfig, "DataBasePath");

        // 加载授权文件
        if (!OfdSdkInit.cnofdSdkInit(System.getProperty("user.dir") + File.separator + cfgLicenseFile)) {
            // 授权失败退出
            return;
        }
        
        // 工作目录
        OfdSdkInit.setTempDir(cfgTempDir);
        OfdSdkInit.setWorkDir(cfgWorkDir);
        // 字体初始化
        OfdSdkInit.fontInit(null);
        
        // 转换引擎及程序路径设置
        cfgOfficeEngine = ConfigUtil.getConfigKey(propConfig, "OfficeEngine");
        cfgHtmlEngine = ConfigUtil.getConfigKey(propConfig, "HtmlEngine");

        OfdSdkInit.setOfficeEngine(cfgOfficeEngine);
        OfdSdkInit.setHtmlEngine(cfgHtmlEngine);

        if (cfgOfficeEngine == "LibreOffice") {
            OfdSdkInit.setLibreOfficePath(ConfigUtil.getConfigKey(propConfig, "LibreOfficePath"));
        }
        
        switch (cfgHtmlEngine) {
        case "Cdp4j":
            OfdSdkInit.setChromePath(ConfigUtil.getConfigKey(propConfig, "ChromePath"));
            break;
        default:
            //
        }


        logger.info("02-完成初始化：" + (System.currentTimeMillis() - start));

        sDataPath = cfgDataBasePath + "ToOFD/";
        sOutPath = cfgDataBasePath + "ToOFD/";

        // PDF转OFD、Office转PDF、Office转OFD
        try {
            // Pdf转Ofd
            pdf2Ofd();

            // Word转Pdf
            word2pdf();

            // Excel转Pdf
            excel2pdf();

            // PPT转Pdf
            ppt2pdf();

            // Word转Ofd
            word2ofd();

            // Excel转Ofd
            excel2ofd();

            // PPT转Ofd
            ppt2ofd();

            // Html转Pdf
            html2pdf();
            // Html转Ofd
            html2ofd();
            // URL转Pdf
            url2pdf();
            // URL转Ofd
            url2ofd();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //
        }
    }

    /**
     * Pdf转Ofd
     */
    public static void pdf2Ofd() {
        ResultInfo result = new ResultInfo();

        logger.info("05-开始Pdf转Ofd：" + (System.currentTimeMillis() - start));
        result = OfdConvertorAgent.pdf2ofd(sDataPath + "pdf-data-cnofd.pdf", sOutPath + "pdf-data-cnofd.ofd");
        logger.info("05-结束Pdf转Ofd：" + (System.currentTimeMillis() - start) + " " + result.getMessage());
    }

    /**
     * Word转Pdf
     */
    public static void word2pdf() {
        ResultInfo result = new ResultInfo();

        logger.info("03-开始Word转PDF：" + (System.currentTimeMillis() - start));
        result = PdfConvertorAgent.word2pdf(sDataPath + "word-data-test.docx", sOutPath + "word-data-test.pdf");
        logger.info("03-结束Word转PDF：" + (System.currentTimeMillis() - start) + " " + result.getMessage());
    }

    /**
     * Excel转Pdf
     */
    public static void excel2pdf() {
        ResultInfo result = new ResultInfo();

        logger.info("03-开始Excel转Pdf：" + (System.currentTimeMillis() - start));
        result = PdfConvertorAgent.excel2pdf(sDataPath + "excel-data-test.xlsx", sOutPath + "excel-data-test.pdf");
        logger.info("03-结束Excel转Pdf：" + (System.currentTimeMillis() - start) + result.getMessage());
    }

    /**
     * PPT转Pdf
     */
    public static void ppt2pdf() {
        ResultInfo result = new ResultInfo();

        logger.info("03-开始PPT转Pdf：" + (System.currentTimeMillis() - start));
        result = PdfConvertorAgent.ppt2pdf(sDataPath + "ppt-data-test.pptx", sOutPath + "ppt-data-test.pdf");
        logger.info("03-结束PPT转Pdf：" + (System.currentTimeMillis() - start) + result.getMessage());
    }

    /**
     * Word转Ofd
     */
    public static void word2ofd() {
        ResultInfo result = new ResultInfo();

        logger.info("06-开始Word转Ofd：" + (System.currentTimeMillis() - start));
        result = OfdConvertorAgent.word2ofd(sDataPath + "word-data-test.docx", sOutPath + "word-data-test.ofd");
        logger.info("06-结束Word转Ofd：" + (System.currentTimeMillis() - start) + " " + result.getMessage());
    }

    /**
     * Excel转Ofd
     */
    public static void excel2ofd() {
        ResultInfo result = new ResultInfo();

        logger.info("07-开始Excel转Ofd：" + (System.currentTimeMillis() - start));
        result = OfdConvertorAgent.excel2ofd(sDataPath + "excel-data-test.xlsx", sOutPath + "excel-data-test.ofd");
        logger.info("07-结束Excel转Ofd：" + (System.currentTimeMillis() - start) + " " + result.getMessage());
    }

    /**
     * PPT转Ofd
     */
    public static void ppt2ofd() {
        ResultInfo result = new ResultInfo();

        logger.info("08-开始PPT转Ofd：" + (System.currentTimeMillis() - start));
        result = OfdConvertorAgent.ppt2ofd(sDataPath + "ppt-data-test.pptx", sOutPath + "ppt-data-test.ofd");
        logger.info("08-结束PPT转Ofd：" + (System.currentTimeMillis() - start) + " " + result.getMessage());
    }
    
    /**
     * Html转Pdf
     */
    public static void html2pdf() {
        ResultInfo result = new ResultInfo();

        logger.info("09-开始Html转PDF：" + (System.currentTimeMillis() - start));           
        result = PdfConvertorAgent.html2pdf(sDataPath + "html-data-test.html", sOutPath + "html-data-test.pdf");
        logger.info("09-结束Html转PDF：" + (System.currentTimeMillis() - start) + result.getMessage()); 
    }
    
    /**
     * Html转Ofd
     */
    public static void html2ofd() {
        ResultInfo result = new ResultInfo();

        logger.info("10-开始Html转OFD：" + (System.currentTimeMillis() - start));           
        result = OfdConvertorAgent.html2ofd(sDataPath + "html-data-test.html", sOutPath + "html-data-test.ofd");
        logger.info("10-结束Html转OFD：" + (System.currentTimeMillis() - start) + result.getMessage()); 
    }
    
    /**
     * URL转Pdf
     */
    public static void url2pdf() {
        ResultInfo result = new ResultInfo();

        logger.info("11-开始URL转PDF：" + (System.currentTimeMillis() - start));           
        result = PdfConvertorAgent.url2pdf("https://baidu.com/", sOutPath + "url-data-baidu.pdf");
        logger.info("11-结束URL转PDF：" + (System.currentTimeMillis() - start) + result.getMessage()); 
    }
    
    /**
     * URL转Ofd
     */
    public static void url2ofd() {
        ResultInfo result = new ResultInfo();

        logger.info("12-开始URL转OFD：" + (System.currentTimeMillis() - start));           
        result = OfdConvertorAgent.url2ofd("https://baidu.com/", sOutPath + "url-data-baidu.ofd", true);
        logger.info("12-结束URL转OFD：" + (System.currentTimeMillis() - start) + result.getMessage()); 
    }
}